﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.FtpClient;
using System.Text;
using System.Threading;

namespace ConsoleFtp
{
    public class FtpManager
    {
        private const string Anonymouse = "anonymous";
        private readonly ManualResetEvent _mReset = new ManualResetEvent(false);

        public void BeginConnect(string host, int port, string username = Anonymouse, string password = Anonymouse)
        {
            using (var conn = new FtpClient())
            {
                _mReset.Reset();
                conn.DataConnectionType = FtpDataConnectionType.PORT;
                conn.Host = host;
                conn.Port = port;
                conn.Credentials = new NetworkCredential(username, password);
                conn.BeginConnect(ConnectCallback, conn);
                _mReset.WaitOne();
                conn.Disconnect();
            }
        }

        private void ConnectCallback(IAsyncResult ar)
        {
            var conn = ar.AsyncState as FtpClient;

            try
            {
                if (conn == null)
                {
                    Console.WriteLine($"连接失败：{conn.Host}");
                }
                conn.EndConnect(ar);
                Console.WriteLine($"连接成功：{conn.Host}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"连接失败：{conn.Host}\r\n异常：{ex}");
            }
            finally
            {
                _mReset.Set();
            }
        }

        public void BeginBeginGetListing(string host, int port, string username = Anonymouse,
          string password = Anonymouse)
        {
            using (var conn = new FtpClient())
            {
                try
                {
                    _mReset.Reset();
                    conn.DataConnectionType = FtpDataConnectionType.PORT;
                    conn.Host = host;
                    conn.Port = port;
                    conn.Credentials = new NetworkCredential(username, password);
                    conn.Connect();
                    conn.BeginGetListing(BeginBeginGetListingCallback, conn);
                    _mReset.WaitOne();
                    conn.Disconnect();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }

        private void BeginBeginGetListingCallback(IAsyncResult ar)
        {
            var conn = ar.AsyncState as FtpClient;
            try
            {
                if (conn == null)
                {
                    Console.WriteLine("The FtpControlConnection object is null!");
                    return;
                }
                Console.WriteLine("获取文件列表");
                var files = conn.EndGetListing(ar);
                var ftpfiles = files.ToList();
                Console.WriteLine(ftpfiles.ToString()); 
            }
            catch (Exception ex)
            {
                Console.WriteLine($"文件列表获取失败，异常：{ex}");
            }
            finally
            {
                _mReset.Set();
            }
        }
    }
}
